/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.tree.mode;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

import twaver.CheckableFilter;
import twaver.Element;
import twaver.TWaverConst;
import twaver.VisibleFilter;
import twaver.list.TList;
import twaver.tree.TTree;
import demo.DemoNode;
import demo.DemoNodeFactory.Category;

public class TreeControlPanel extends JPanel{
    // only demo nodes checkable
    private CheckableFilter demoNodeFilter = new CheckableFilter(){
    	public boolean isCheckable(Element element) {
    		if(element instanceof DemoNode){
    			return true;
    		}
    		return false;
    	}
    };
    
    // only category nodes checkable
    private CheckableFilter categoryFilter = new CheckableFilter(){
    	public boolean isCheckable(Element element) {
    		if(element instanceof Category){
    			return true;
    		}
    		return false;
    	}
    };
    
    // only network nodes checkable
    private CheckableFilter networkFilter = new CheckableFilter(){
    	public boolean isCheckable(Element element) {
    		if("Network".equals(element.getName())){
    			return true;
    		}
    		if(element.getParent() != null){
    			return this.isCheckable(element.getParent());
    		}
    		return false;
    	}
    };
    
    // all nodes are checkable
    private CheckableFilter allFilter = new CheckableFilter(){
    	public boolean isCheckable(Element element) {
    		return true;
    	}
    };
	
	private TTree tree;
	public TreeControlPanel(TTree tree){
		this.tree = tree;
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
	    panel.add(createSelectionModePanel());
	    panel.add(createFilterPanel());
	    panel.add(createStylePanel());
	    panel.add(createEnablePanel());
		JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panel, createList());
		this.setLayout(new BorderLayout());
		this.add(split);
	}

	private JPanel createEnablePanel(){
	    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
	    final JCheckBox enableTristate = new JCheckBox("Tristate", tree.isEnableTristateCheckBox());
	    final JCheckBox checkedWhenNotSpecified = new JCheckBox("Checked When Not Specified", tree.isCheckedWhenNotSpecified());
	    final JCheckBox iconVisible = new JCheckBox("Icon", tree.isIconVisible());
	    final JCheckBox selectedWhenChecked = new JCheckBox("Selected When Checked", tree.isPaintSelectedStateWhenChecked());
	    panel.add(enableTristate);
	    panel.add(checkedWhenNotSpecified);
	    panel.add(iconVisible);
	    panel.add(selectedWhenChecked);
	    enableTristate.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				tree.setEnableTristateCheckBox(enableTristate.isSelected());
			}
	    });
	    checkedWhenNotSpecified.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent e) {
	    		tree.setCheckedWhenNotSpecified(checkedWhenNotSpecified.isSelected());
	    	}
	    });
	    iconVisible.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent e) {
	    		tree.setIconVisible(iconVisible.isSelected());
	    	}
	    });
	    selectedWhenChecked.addActionListener(new ActionListener(){
	    	public void actionPerformed(ActionEvent e) {
	    		tree.setPaintSelectedStateWhenChecked(selectedWhenChecked.isSelected());
	    	}
	    });
	    return panel;
	}
	
	private JScrollPane createList(){
		final TList list = new TList(tree.getDataBox());
		list.addVisibleFilter(new VisibleFilter(){
			public boolean isVisible(Element element) {
				return element.isSelected();
			}
		});
		list.setAutoSyncSelectionFromViewToDataBox(false);
		return new JScrollPane(list);
	}
	
	
	private JPanel createSelectionModePanel(){
	    JPanel modePanel = new JPanel(new GridLayout(5, 1));
	    modePanel.setBorder(BorderFactory.createTitledBorder("Selection Mode"));
	    ButtonGroup group = new ButtonGroup();
	    modePanel.add(createModeButton("Default Selection", TTree.DEFAULT_SELECTION, group));
	    modePanel.add(createModeButton("Check Selection", TTree.CHECK_SELECTION, group));
	    modePanel.add(createModeButton("Check Children Selection", TTree.CHECK_CHILDREN_SELECTION, group));
	    modePanel.add(createModeButton("Check Descendant Selection", TTree.CHECK_DESCENDANT_SELECTION, group));
	    modePanel.add(createModeButton("Check Descendant Ancestor Selection", TTree.CHECK_DESCENDANT_ANCESTOR_SELECTION, group));
	    return modePanel;
	}
	
	private JRadioButton createModeButton(String name, final int selectionMode, ButtonGroup group){
	    JRadioButton button = new JRadioButton(name);
	    button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				tree.setTTreeSelectionMode(selectionMode);
				tree.getDataBox().getSelectionModel().clearSelection();
			}
	    });
	    group.add(button);
	    if(TTree.CHECK_SELECTION == selectionMode){
			tree.setTTreeSelectionMode(TTree.CHECK_SELECTION);
	    	button.setSelected(true);
	    }
	    return button;
	}
	
	private JPanel createFilterPanel(){
	    JPanel filterPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
	    filterPanel.setBorder(BorderFactory.createTitledBorder("Checkable Filter"));
	    ButtonGroup group = new ButtonGroup();
	    filterPanel.add(createFilterButton("Leaf", this.demoNodeFilter, group));
	    filterPanel.add(createFilterButton("Category", this.categoryFilter, group));
	    filterPanel.add(createFilterButton("Network", this.networkFilter, group));
	    filterPanel.add(createFilterButton("All", this.allFilter, group));
	    return filterPanel;
	}
	
	private JRadioButton createFilterButton(String name, final CheckableFilter filter, ButtonGroup group){
	    JRadioButton button = new JRadioButton(name);
	    button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				tree.setCheckableFilter(filter);
				tree.getDataBox().getSelectionModel().clearSelection();
			}
	    });
	    group.add(button);
	    if(filter == this.demoNodeFilter){
	    	tree.setCheckableFilter(this.demoNodeFilter);
	    	button.setSelected(true);
	    }
	    return button;
	}
	
	private JPanel createStylePanel(){
	    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
	    panel.setBorder(BorderFactory.createTitledBorder("Uncheckable Style"));
	    ButtonGroup group = new ButtonGroup();
	    panel.add(createStyleButton("None", TWaverConst.UNCHECKABLE_STYLE_NONE, group));
	    panel.add(createStyleButton("Blank", TWaverConst.UNCHECKABLE_STYLE_BLANK, group));
	    panel.add(createStyleButton("Disable", TWaverConst.UNCHECKABLE_STYLE_DISABLE, group));
	    return panel;
	}
	
	private JRadioButton createStyleButton(String name, final int style, ButtonGroup group){
	    JRadioButton button = new JRadioButton(name);
	    button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				tree.setUncheckableStyle(style);
			}
	    });
	    if(tree.getUncheckableStyle() == style){
	    	button.setSelected(true);
	    }
	    group.add(button);
	    return button;
	}
	
	
}